package com.dmall.leetcode;

/**
 * LeetCode：203-移除链表元素
 * 使用递归的方式删除链表中指定的元素
 * @author xiao1.wang@dmall.com
 * @date 2019-03-24 00:43:30
 */
public class Solution3 {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }

        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }

    public ListNode removeElements1(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode res = removeElements1(head.next, val);
        if (head.val == val) {
            return res;
        } else {
            head.next = res;
            return head;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 6, 3, 4, 5, 6};
        ListNode head = new ListNode(arr);
        System.out.println(head);

        ListNode listNode = new Solution3().removeElements1(head, 6);
        System.out.println(listNode);
    }
}
